<?php
namespace App\Repositories\Admin;

use App\Models\Admin\SearchLogModel;
use App\Repositories\BaseRepository;

class SearchLogRepository extends BaseRepository
{
    protected $repository;

    public function __construct()
    {
        $this->repository = new SearchLogModel();
    }

    public function getIndexs($uid=0)
    {
        $query = $this->repository->where('word','<>','');
        if ($uid) {
            $query = $query->where('uid',$uid);
        }
        return $query->orderBy('id','desc')->paginate(5);
    }

    public function getApiIndexs($uid=0)
    {
        $query = $this->repository
            ->where('word','<>','');
        if ($uid) {
            $query = $query->where('uid',$uid);
        }
        return $query->orderBy('id','desc')
            ->skip(0)
            ->take(5)
            ->get();
    }

    public function setApiAdd($uid,$word)
    {
        $model = $this->repository
            ->where('uid',$uid)
            ->where('word',$word)
            ->first();
        if (!$model) {
            $data = array(
                'uid' => $uid,
                'word' => $word,
                'created_at' => time(),
            );
            return $this->repository->insertGetId($data);
        }
        $this->repository
            ->where('uid',$uid)
            ->where('word',$word)
            ->update(['num' => $model->num+1]);
        return $model->id;
    }

    public function getShowOne($id)
    {
        return $this->repository->where('id',$id)->first();
    }
}